<template>
  <div class='my-login'>
    <div class="logo">
      <img src="@/assets/logo.png" alt="" />
    </div>
    <div class="login-form">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px">
        <el-form-item label="邮箱地址" prop="email">
          <el-input type="email" v-model="ruleForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-button type="primary" @click="submitForm" style="width:100%">登录</el-button>
      </el-form>
      <el-divider></el-divider>
    </div>
    <div class="login-footer">
      <p>还没有账号？ <router-link to="/register"> 点此注册</router-link>
      </p>
    </div>
  </div>
</template>

<script>
export default {
  name: 'MyLogin',
  data () {
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('密码不能为空'))
      }
      if (!(/[\S]{6,}/.test(value))) {
        callback(new Error('密码不能小于6位'))
      }
      callback()
    }
    return {
      ruleForm: {
        email: '',
        password: ''
      },
      rules: {
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$store.dispatch('user/loginActions', this.ruleForm).then(res => {
            if (res.code === 0) {
              this.$message.success(res.msg)
              // 登录成功跳转
              if (this.$route.query.redirect) {
                this.$router.replace(decodeURIComponent(this.$route.query.redirect) || '/')
              } else {
                this.$router.replace('/')
              }
            } else {
              this.$message.error(res.msg)
            }
          }).catch(err => {
            console.log(err)
            this.$message.error(err.msg)
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang='less' scoped>
.my-login {
  position: absolute;
  width: 500px;
  height: 460px;
  left: 50%;
  top: 50px;
  transform: translateX(-50%);
  background-color: #fff;
  text-align: center;
  padding: 30px 30px 30px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.2);
  border-radius: 10px;
}

.logo {
  padding: 50px 0;

  &>img {
    width: 300px;
    height: 100px;
  }
}

.login-form {
  padding: 0 20px;
}

:deep(.login-form) {
  margin-bottom: 10px;
}

.login-footer {
  text-align: center;
  font-size: 14px;

  a {
    color: #ce6e70;
  }
}

.el-divider--horizontal {
  margin: 12px 0;
}

:deep(.el-form-item__label) {
  text-align: left;
}
</style>
